package com.clz.workorder.fragment

import com.alibaba.android.arouter.facade.annotation.Route
import com.annimon.stream.Collectors
import com.annimon.stream.Stream
import com.blankj.utilcode.util.ObjectUtils
import com.clz.workorder.BR
import com.clz.workorder.R
import com.clz.workorder.databinding.FragmentComplaintTurnBinding
import com.clz.workorder.viewmodel.ComplaintTurnViewModel
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.config.ComplaintType
import com.czl.base.config.OrderType
import com.czl.base.data.bean.ComplaintDeptBean
import com.czl.base.data.bean.OrderTypeBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DialogHelper

@Route(path = AppConstants.Router.Complaint.F_COMPLAINT_TURN)
class ComplaintTurnFragment: BaseFragment<FragmentComplaintTurnBinding, ComplaintTurnViewModel>() {
    private var deptList: List<ComplaintDeptBean>?=null
    override fun initContentView(): Int {
        return R.layout.fragment_complaint_turn
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("转单")
        if (arguments != null) {
            viewModel.no = requireArguments().getString(AppConstants.BundleKey.ORDER_NO_KEY, "")
            viewModel.repairType =
                requireArguments().getString(AppConstants.BundleKey.REPAIR_TYPE, "")
            viewModel.subTypeId =
                requireArguments().getString(AppConstants.BundleKey.SUB_TYPE_ID, "")
            viewModel.queryWorkOrderType()
            viewModel.getDeptByCompanyId(false)
        }
    }

    override fun initViewObservable() {
        viewModel.uc.queryDeptCompleteEvent.observe(this,{
            list->
                deptList=list
                showDeptListPop()

        })
        viewModel.uc.queryUserCompleteEvent.observe(this, {
            if (ObjectUtils.isEmpty(it)) {
                showNormalToast("处理人列表为空！")
                return@observe
            }
            DialogHelper.showBottomListDialog(
                requireContext(),
                Stream.of(it).map { t -> t.userName }.withoutNulls()
                    .collect(Collectors.toList()) as ArrayList<String>
            ) { position, text ->
                viewModel.repairerName.set(text)
                viewModel.repairerId = it[position].userId
            }
        })
        viewModel.uc.turnOrderEvent.observe(this, {
            LiveBusCenter.postRefreshComplaintEvent(ComplaintType.TURN)
            back()
        })
    }

    private fun showDeptListPop(){
        if (ObjectUtils.isEmpty(deptList)){
            showNormalToast("无处理单位可选")
            return
        }
        deptList?.let {
            DialogHelper.showBottomListDialog(
                requireContext(),
                Stream.of(it).map { t -> t.deptName }.withoutNulls()
                    .collect(Collectors.toList()) as ArrayList<String>
            ) { position, text ->
                viewModel.deptName.set(it[position].deptName)
                viewModel.deptId = it[position].deptId.toString()
                viewModel.repairerName.set("")
                viewModel.repairerId = ""
            }
        }
    }
}